limsEquiptFileAcqConfig.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-equipt-file-acq-config-edit-list-grid',
            url : limsEquiptFileAcqConfig.baseUrl + '/queries/raw',
            service : 'limsEquiptFileAcqConfigServiceImpl',
            dbTable : 'T_LIMS_EQUIPT_FILE_ACQ_CONFIG',
            columns : limsEquiptFileAcqConfig.getEditListColumns(),
            toolbar : _this.getGridBtnToolbar(),
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                }
            },
            onRowActive : function (index, row){
                debugger
                _this.param.id = row.id;
                Gikam.getAsyncComp('lims-equipt-file-acq-config-detail-base-info-form').done(function (baseInfo) {
                    if (Gikam.isEmpty(row)) {
                        baseInfo.setOptions({
                            autoSave: false,
                            readonly: true,
                            url: ""
                        })
                        baseInfo.cleanData();
                        baseInfo.initReadonly();
                    } else {
                        baseInfo.setOptions({
                            autoSave: true,
                            url: Gikam.printf(_this.urlManager.baseInfoForm, {
                                id: _this.param.id
                            })
                        })
                        baseInfo.refresh();
                    }
                });
            }
        }
    },

    getBaseInfoTab : function() {
        var _this = this;
        return {
            type : 'tab',
            panels : [{
                title : 'BASEMODULE.TRM.EQUIPT.EQUIPTFILEACQS.TAB.BASE_INFO',
                items : [this.getTabBtnToolbar(),{
                    type : 'form',
                    url : "",
                    service : 'limsEquiptFileAcqConfigServiceImpl',
                    dbTable : 'T_LIMS_EQUIPT_FILE_ACQ_CONFIG',
                    id : _this.idManager.limsEquiptFileAcqConfigDetailBaseInfoForm,
                    panels : [{
                        title : 'BASEMODULE.TRM.EQUIPT.EQUIPTFILEACQS.TAB.BASE_OPTION',
                        id : _this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormBasePanel,
                        fields : limsEquiptFileAcqConfig.getBaseInfoFormFields()
                    }, {
                        title: 'BASEMODULE.TRM.EQUIPT.EQUIPTFILEACQS.TAB.FILE_OPTION',
                        id: _this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormFilePanel,
                        fields: limsEquiptFileAcqConfig.getFileFormFields()
                    }, {
                        title: 'BASEMODULE.TRM.EQUIPT.EQUIPTFILEACQS.TAB.DB_OPTION',
                        id: _this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormDbPanel,
                        fields: limsEquiptFileAcqConfig.getDbFormFields()
                    }],
                    // fields : limsEquiptFileAcqConfig.getBaseInfoFormFields(),
                    columns : 3,
                    onLoadSuccess : function (data) {
                        _this.showAcqConfigFields();
                    },
                    onUpdated : function (data) {
                        _this.showAcqConfigFields();
                    },
                    onValidating : function(data,fieldObj){
                        if(fieldObj.field=='filePath' && !Gikam.isEmpty(data.filePath)){
                            var pattern = new RegExp('^/');
                            if (!pattern.test(data.filePath)) {
                                return {
                                    result: 'false',
                                    message: '路径必须要以“/”开头'
                                }
                            }
                        }else if(fieldObj.field=='savePath' && !Gikam.isEmpty(data.savePath)){
                            var pattern = new RegExp('^/');
                            if (!pattern.test(data.savePath)) {
                                return {
                                    result: 'false',
                                    message: '路径必须要以“/”开头'
                                }
                            }
                        }
                    }
                } ]
            } ]
        }
    },

    //显示隐藏
    showAcqConfigFields : function () {
        var _this = this;
        Gikam.getAsyncComp(_this.idManager.limsEquiptFileAcqConfigDetailBaseInfoForm).done(function (form) {
            var data = form.getData();
            if (data.acqMode == 'database') {
                form.showPanelById([_this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormDbPanel]);
                form.hidePanelById([_this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormFilePanel]);
            } else if (data.acqMode == 'ftp' || data.acqMode == 'shareDocV1' || data.acqMode == 'shareDocV23' ) {
                if (data.exectorName == 'SHAtmAcqExector') { //上海奥拓美用access数据库文件
                    form.showPanelById([_this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormFilePanel,
                        _this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormDbPanel]);
                } else {
                    form.showPanelById([_this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormFilePanel]);
                    form.hidePanelById([_this.idManager.limsEquiptFileAcqConfigDetailBaseInfoFormDbPanel]);
                }
            }
            _this.showHideInstLayout(data.acqCategory);
        });
    },

    getGridBtnToolbar : function () {
        return [{
            type : 'button',
            text : 'GIKAM.BUTTON.INSERT',
            icon : 'add',
            onClick : function() {
                Gikam.create('modal', {
                    title : Gikam.choose.getConfig('equipment').title,
                    url : Gikam.choose.getConfig('equipment').url + Gikam.param({
                        target_filter: encodeURI(JSON.stringify({
                            type: 'unassigned',
                            targetTable: 'T_LIMS_EQUIPT_FILE_ACQ_CONFIG',
                            targetMatchColumn: 'equiptId',
                            thisMatchColumn: 'ID',
                        }))
                    }),
                    onAfterClose : function(rows) {
                        if (Gikam.isEmpty(rows)) {
                            return;
                        }
                        inserts = [];
                        rows.forEach(function (row) {
                            inserts.push({
                                equiptId : row.id,
                                equiptCode : row.equipCode
                            })
                        })

                        Gikam.getComp("lims-equipt-file-acq-config-edit-list-grid").insert(Gikam.printf(limsEquiptFileAcqConfig.baseUrl), inserts ).done(function() {
                            Gikam.getComp("lims-equipt-file-acq-config-edit-list-grid").refresh();
                        });
                    }
                })
            }
        }, {
            type : 'button',
            text : 'GIKAM.BUTTON.DELETE',
            icon : 'remove-row',
            onClick : function() {
                Gikam.getComp('lims-equipt-file-acq-config-edit-list-grid').deleteRows(limsEquiptFileAcqConfig.baseUrl);
            }
        }]
    },

    getTabBtnToolbar : function(){
        return {
            type : 'btnToolbar',
            items: [{
                type : 'button',
                text : 'MODULE.COMMON.EQUIPMENTS.CONNECTTEST',
                icon : 'submit', color: 'success',
                onClick : function() {
                    var connect = Gikam.getComp('lims-equipt-file-acq-config-detail-base-info-form').getData();
                    if(Gikam.isEmpty(connect) || Gikam.isEmpty(connect.id)){
                        Gikam.alert('请新增设备数据源！');
                        return;
                    }
                    if (Gikam.isEmpty(connect.ip) || Gikam.isEmpty(connect.port)){
                        Gikam.alert('请填写完善配置信息！');
                        return;
                    }
                    if(Gikam.isEmpty(connect.acqMode)){
                        Gikam.alert('请配置文件读取方式！');
                        return;
                    }
                    if (Gikam.isEmpty(connect.filePath)){
                        Gikam.alert('请填写文件获取路径！');
                        return;
                    }
                    Gikam.post(limsEquiptFileAcqConfig.baseUrl + '/connect', Gikam.getJsonWrapper(null, ['limsEquiptFileAcqConfigServiceImpl', [connect]])).done(function(result) {
                        debugger
                        if (result == true){
                            Gikam.alert('连接成功');
                        }else {
                            Gikam.alert('连接失败');
                        }
                    });
                }
            },{
                type : 'button',
                text : 'MODULE.COMMON.EQUIPMENTS.COLLECTFILE',
                icon : 'submit', color: 'success',
                onClick : function() {
                    var connect = Gikam.getComp('lims-equipt-file-acq-config-detail-base-info-form').getData();
                    if(Gikam.isEmpty(connect) || Gikam.isEmpty(connect.id)){
                        Gikam.alert('请新增设备数据源！');
                        return;
                    }
                    if (Gikam.isEmpty(connect.ip) || Gikam.isEmpty(connect.port)
                        || Gikam.isEmpty(connect.filePath) || Gikam.isEmpty(connect.savePath)){
                        Gikam.alert('请填写完善配置信息！');
                        return;
                    }
                    if (Gikam.isEmpty(connect.password)){
                        Gikam.alert('请填写密码！');
                        return;
                    }
                    if(Gikam.isEmpty(connect.acqMode)){
                        Gikam.alert('请配置文件读取方式！');
                        return;
                    }
                    Gikam.post(limsEquiptFileAcqConfig.baseUrl + '/acqFiles', Gikam.getJsonWrapper(null, ['limsEquiptFileAcqConfigServiceImpl', [connect]])).done(function() {
                        Gikam.toast('成功加入采集任务');
                    });
                }
            }]
        }
    },

    urlManager: {
        baseInfoForm: limsEquiptFileAcqConfig.baseUrl + '/{id}',
    },

    idManager : {
        limsEquiptFileAcqConfigDetailBaseInfoForm : 'lims-equipt-file-acq-config-detail-base-info-form',
        limsEquiptFileAcqConfigDetailBaseInfoFormBasePanel : 'lims-equipt-file-acq-config-detail-base-info-form-base-panel',
        limsEquiptFileAcqConfigDetailBaseInfoFormFilePanel : 'lims-equipt-file-acq-config-detail-base-info-form-file-panel',
        limsEquiptFileAcqConfigDetailBaseInfoFormDbPanel : 'lims-equipt-file-acq-config-detail-base-info-form-db-panel',
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-equipt-file-acq-config-edit-list-layout',
            renderTo : workspace.window.$dom,
            west : {
                width : '30%',
                items : [this.getGridParam()]
            },
            center : {
                items : [this.getBaseInfoTab()]
            }
        });
    },

    init : function(param) {
        this.param = {};
        this.create();
    }
};
